﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.InteropServices;

namespace Vms.Web.Lib
{
    /// <summary>
    /// Create date: 11/03/2013.
    /// Author: Hai Phan Van.
    /// Description: Generate based BX-IV Dynamic Library User Manual.
    /// </summary>
    public static class BXIV
    {
        #region PUBLIC STATIC METHODS
        /// <summary>
        /// Add/Set Display Screen.
        /// </summary>
        /// <param name="nControlType">Controller Mode</param>
        /// <param name="nScreenNo">Screen No, Start from 1</param>
        /// <param name="nWidth">Width</param>
        /// <param name="nHeight">Height</param>
        /// <param name="nScreenType">Type of display: 1 Single Color/ 2 Dual Color/ 3 RGB Color.</param>
        /// <param name="nPixelMode">Pixels type; 1: R+G 2:G+R; This parameters only support dual-color; Default:2.</param>
        /// <param name="nDataDA">Data polarity: 0x00; Negative:0x01; Positive; Default: 0;</param>
        /// <param name="nDataOE">OE polarity; 0x00: OE low; 0x01; OE high; Default: 0.</param>
        /// <param name="nRowOrder">Row order mode; 0:normal; 1: add one line; 2: delete one line; Default:0.</param>
        /// <param name="nFreqPar">Scan frequency: 0~6; Default: 0</param>
        /// <param name="pCom">Serial port name; Valid in serial port communication mode; Example: COM1</param>
        /// <param name="nBaud">Serial port baud rate: currently with 9600, 57600; Default: 57600; Note: BX-3T only support 9600 baud rate.</param>
        /// <param name="pSocketIP">Controller IP address</param>
        /// <param name="nSocketPor">Controller Ethernet port</param>
        /// <param name="pScreenStatusFile">The saved INI file name which used for saving found display screenstatus parameters.</param>
        /// <returns>
        /// RETURN_NOERROR ： Return without error;
        /// RETURN_ERROR_RA_SCREEN NO ： The information of this screen existed. If users want to reset,please delete this screen by DeleteScreen;
        /// RETURN_ERROR_OT HER: Add parameters error;
        /// </returns>
        [DllImport("BX_IV.dll")]
        public static extern int AddScreen(int nControlType, int nScreenNo,
        int nWidth, int nHeight, int nScreenType, int nPixelMode, int nDataDA,
           int nDataOE, int nRowOrder, int nFreqPar, string pCom, int nBaud,
           string pSocketIP, int nSocketPor, string pScreenStatusFile);

        /// <summary>
        /// Delete specified display screen, and delete all the informations there.
        /// </summary>
        /// <param name="nScreenNo">From number one</param>
        /// <returns>
        /// RETURN_NOERROR: Return without error
        /// RETURN_ERROR_NOFIND_SCREENNO ： No screen in the system
        /// RETURN_ERROR_OTHER ： Other errors
        /// </returns>
        [DllImport("BX_IV.dll")]
        public static extern int DeleteScreen(int nScreenNo);

        /// <summary>
        /// Send the corresponding command to display screen.
        /// </summary>
        /// <param name="nScreenNo">From number one</param>
        /// <param name="nSendMode">Communication mode: serial port: 0; Ethernet mode: 2.</param>
        /// <param name="nSendCmd">
        /// SEND_CMD_PARAMETER =0xA1FF;  Write screen parameters
        /// SEND_CMD_SENDALLPROGRAM = 0x A1F0;  Send all the program information
        /// SEND_CMD_POWERON =0x A2FF;  Forced ON
        /// SEND_CMD_POWEROFF = 0x A2FE;  Forced OFF
        /// SEND_CMD_TIMERPOWERONOFF = 0x A2FD;  Automatically ON/OFF
        /// SEND_CMD_CANCEL_TIMERPOWERONOFF = 0x A2FC;  Delete automatically ON/OFF
        /// SEND_CMD_RESIVETIME = 0x A2FB;  Correction me
        /// SEND_CMD_ADJUSTLIGHT = 0x A2FA;  Adjust brightness
        /// </param>
        /// <param name="nOtherParam1">Other parameters</param>
        /// <returns></returns>
        [DllImport("BX_IV.dll")]
        public static extern int SendScreenInfo(int nScreenNo, int nSendMode, int nSendCmd, int nOtherParam1);

        /// <summary>
        /// Add program on the display screen;
        /// </summary>
        /// <param name="nScreenNo"></param>
        /// <param name="nProgramType"></param>
        /// <param name="nPlayLength"></param>
        /// <param name="nStartYear"></param>
        /// <param name="nStartMonth"></param>
        /// <param name="nStartDay"></param>
        /// <param name="nEndYear"></param>
        /// <param name="nEndMonth"></param>
        /// <param name="nEndDay"></param>
        /// <param name="nMonPlay"></param>
        /// <param name="nTuesPlay"></param>
        /// <param name="nWedPlay"></param>
        /// <param name="nThursPlay"></param>
        /// <param name="bFriPlay"></param>
        /// <param name="nSatPlay"></param>
        /// <param name="nSunPlay"></param>
        /// <param name="nStartHour"></param>
        /// <param name="nStartMinute"></param>
        /// <param name="nEndHour"></param>
        /// <param name="nEndMinute"></param>
        /// <returns></returns>
        [DllImport("BX_IV.dll")]
        public static extern int AddScreenProgram(int nScreenNo, int nProgramType, int nPlayLength,
            int nStartYear, int nStartMonth, int nStartDay, int nEndYear, int nEndMonth, int nEndDay,
            int nMonPlay, int nTuesPlay, int nWedPlay, int nThursPlay, int bFriPlay, int nSatPlay, int nSunPlay,
            int nStartHour, int nStartMinute, int nEndHour, int nEndMinute);

        /// <summary>
        /// Delete the program, and delete all the information there.
        /// </summary>
        /// <param name="nScreenNo">From number one</param>
        /// <param name="nProgramOrd">Program number; From number zero</param>
        /// <returns></returns>
        [DllImport("BX_IV.dll")]
        public static extern int DeleteScreenProgram(int nScreenNo, int nProgramOrd);

        /// <summary>
        /// Delete the specified area, and delete all the informations there.
        /// </summary>
        /// <param name="nScreenNo">From number one</param>
        /// <param name="nProgramOrd">Program number; From number zero</param>
        /// <param name="nAreaOrd">Area number; From number zero</param>
        /// <returns>
        /// RETURN_NOERROR: Return without error
        /// RETURN_ERROR_AERETYPE: Area type error when add/delete the Image/Text area file.
        /// RETURN_ERROR_NOFIND_SCREENNO ： Do not find the display screen in the system;
        /// RETURN_ERROR_NOFIND_PROGRAM ： Do not find the specified program of the display screen in the system;
        /// RETURN_ERROR_NOFIND_AREA ： Do not find the specified area in the program of display screen.
        /// RETURN_ERROR_OTHER ： Other errors
        /// </returns>
        [DllImport("BX_IV.dll")]
        public static extern int DeleteScreenProgramArea(int nScreenNo, int nProgramOrd, int nAreaOrd);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="nScreenNo"></param>
        /// <param name="nProgramOrd"></param>
        /// <param name="nX"></param>
        /// <param name="nY"></param>
        /// <param name="nWidth"></param>
        /// <param name="nHeight"></param>
        /// <returns></returns>
        [DllImport("BX_IV.dll")]
        public static extern int AddScreenProgramBmpTextArea(int nScreenNo, int nProgramOrd, int nX, int nY,
            int nWidth, int nHeight);

        /// <summary>
        /// Add file on the specified area in program of the specified display screen;
        /// </summary>
        /// <param name="nScreenNo"></param>
        /// <param name="nProgramOrd"></param>
        /// <param name="nAreaOrd"></param>
        /// <param name="pFileName"></param>
        /// <param name="nShowSingle"></param>
        /// <param name="pFontName"></param>
        /// <param name="nFontSize"></param>
        /// <param name="nBold"></param>
        /// <param name="nFontColor"></param>
        /// <param name="nStunt"></param>
        /// <param name="nRunSpeed"></param>
        /// <param name="nShowTime"></param>
        /// <returns></returns>
        [DllImport("BX_IV.dll")]
        public static extern int AddScreenProgramAreaBmpTextFile(int nScreenNo, int nProgramOrd, int nAreaOrd,
        string pFileName, int nShowSingle, string pFontName, int nFontSize, int nBold, int nFontColor,
            int nStunt, int nRunSpeed, int nShowTime);

        /// <summary>
        /// Delete the specified area, and delete all the informations there.
        /// </summary>
        /// <param name="nScreenNo">From number one</param>
        /// <param name="nProgramOrd">Program number; From number zero</param>
        /// <param name="nAreaOrd">Area number; From number zero</param>
        /// <param name="nFileOrd">Area files number; from number zero</param>
        /// <returns>
        /// RETURN_NOERROR: Return without error
        /// RETURN_ERROR_NOFIND_SCREENNO ： Do not find the display screen in the system;
        /// RETURN_ERROR_NOFIND_PROGRAM ： Do not find the specified program of the display screen in the system;
        /// RETURN_ERROR_NOFIND_AREA ： Do not find the specified area in the program of display
        /// </returns>
        [DllImport("BX_IV.dll")]
        public static extern int DeleteScreenProgramAreaBmpTextFile(int nScreenNo, int nProgramOrd, int nAreaOrd, int nFileOrd);

        /// <summary>
        /// Initialize the dynamic library resources, you must call this function before calling other functions.
        /// </summary>
        /// <param name="AppHandle">App handle</param>
        /// <returns></returns>
        [DllImport("BX_IV.dll")]
        public static extern int InitDLLResource(IntPtr AppHandle);

        /// <summary>
        /// Release dynamic library resources, and exit the program before calling the function .
        /// </summary>
        /// <returns></returns>
        [DllImport("BX_IV.dll")]
        public static extern int FreeDLLResource();

        /// <summary>
        /// Get screen status.
        /// </summary>
        /// <param name="nScreenNo"></param>
        /// <param name="nSendMode"></param>
        /// <returns></returns>
        [DllImport("BX_IV.dll")]
        public static extern int GetScreenStatus(int nScreenNo, int nSendMode);

        /// <summary>
        /// Set the brightness of the screen.
        /// </summary>
        /// <param name="nScreenNo"></param>
        /// <param name="nAjustType"></param>
        /// <param name="nHandleLight"></param>
        /// <param name="nHour1"></param>
        /// <param name="nMinute1"></param>
        /// <param name="nLight1"></param>
        /// <param name="nHour2"></param>
        /// <param name="nMinute2"></param>
        /// <param name="nLight2"></param>
        /// <param name="nHour3"></param>
        /// <param name="nMinute3"></param>
        /// <param name="nLight3"></param>
        /// <param name="nHour4"></param>
        /// <param name="nMinute4"></param>
        /// <param name="nLight4"></param>
        /// <returns></returns>
        [DllImport("BX_IV.dll")]
        public static extern int SetScreenAjustLight(int nScreenNo, int nAjustType, int nHandleLight, 
            int nHour1, int nMinute1, int nLight1, int nHour2, int nMinute2, int nLight2,
            int nHour3, int nMinute3, int nLight3, int nHour4, int nMinute4, int nLight4);
       

        #endregion

        #region RETURN ERRORS

        /// <summary>
        /// regional type of error, the error returns the error of this type in the area to add, delete, graphic zone file type.
        /// </summary>
        public const int RETURN_ERROR_AERETYPE = 0xF7;

        /// <summary>
        /// the display. To reset Please DeleteScreen remove the display and then add;
        /// </summary>
        public const int RETURN_ERROR_RA_SCREENNO = 0xF8;

        /// <summary>
        /// not found an effective zone files (graphic area);
        /// </summary>
        public const int RETURN_ERROR_NOFIND_AREAFILE = 0xF9;

        /// <summary>
        /// not found an effective display area; can use AddScreenProgramBmpTextArea add regional information.
        /// </summary>
        public const int RETURN_ERROR_NOFIND_AREA = 0xFA;

        /// <summary>
        /// not found an effective display program; use AddScreenProgram function to add the specified program
        /// </summary>
        public const int RETURN_ERROR_NOFIND_PROGRAM = 0xFB;

        /// <summary>
        /// system did not find the display; can add display use AddScreen functions        
        /// </summary>
        public const int RETURN_ERROR_NOFIND_SCREENNO = 0xFC;

        /// <summary>
        /// the system is to display communication, later communications;
        /// </summary>
        public const int RETURN_ERROR_NOW_SENDING = 0xFD;

        /// <summary>
        /// other errors;
        /// </summary>
        public const int RETURN_ERROR_OTHER = 0xFF;

        /// <summary>
        /// no error
        /// </summary>
        public const int RETURN_NOERROR = 0;

        #endregion

        #region CONTROLLER TYPE
        //-----------------------------------------------------------------------------
        public const int CONTROLLER_TYPE_3T = 0x10;
        public const int CONTROLLER_TYPE_3A = 0x20;
        public const int CONTROLLER_TYPE_3A1 = 0x21;
        public const int CONTROLLER_TYPE_3A2 = 0x22;
        public const int CONTROLLER_TYPE_3M = 0x30;

        public const int CONTROLLER_TYPE_4A1 = 0x0141;
        public const int CONTROLLER_TYPE_4A2 = 0x0241;
        public const int CONTROLLER_TYPE_4A3 = 0x0341;
        public const int CONTROLLER_TYPE_4AQ = 0x1041;
        public const int CONTROLLER_TYPE_4A = 0x0041;

        public const int CONTROLLER_TYPE_4UT = 0x0445;
        public const int CONTROLLER_TYPE_4U0 = 0x0045;
        public const int CONTROLLER_TYPE_4U1 = 0x0145;
        public const int CONTROLLER_TYPE_4U2 = 0x0245;
        public const int CONTROLLER_TYPE_4U2X = 0x0545;
        public const int CONTROLLER_TYPE_4U3 = 0x0345;

        public const int CONTROLLER_TYPE_4M0 = 0x0242;
        public const int CONTROLLER_TYPE_4M1 = 0x0142;
        public const int CONTROLLER_TYPE_4M = 0x0042;
        public const int CONTROLLER_TYPE_4MC = 0x0C42;
        public const int CONTROLLER_TYPE_4E = 0x0044;
        public const int CONTROLLER_TYPE_4C = 0x0043;
        public const int CONTROLLER_TYPE_4E1 = 0x0144;
        public const int CONTROLLER_TYPE_4EL = 0x0844;

        public const int CONTROLLER_TYPE_5AT = 0x0051;
        public const int CONTROLLER_TYPE_5A0 = 0x0151;
        public const int CONTROLLER_TYPE_5A1 = 0x0251;
        public const int CONTROLLER_TYPE_5A1WIFI = 0x0651;
        public const int CONTROLLER_TYPE_5A2 = 0x0351;
        public const int CONTROLLER_TYPE_5A2RF = 0x0751;
        public const int CONTROLLER_TYPE_5A2WIFI = 0x0851;
        public const int CONTROLLER_TYPE_5A3 = 0x0451;
        public const int CONTROLLER_TYPE_5A4 = 0x0551;
        public const int CONTROLLER_TYPE_5A4RF = 0x1551;
        public const int CONTROLLER_TYPE_5A4WIFI = 0x0851;

        public const int CONTROLLER_TYPE_5M1 = 0x0052;
        public const int CONTROLLER_TYPE_5M2 = 0x0252;
        public const int CONTROLLER_TYPE_5M3 = 0x0353;
        public const int CONTROLLER_TYPE_5M4 = 0x0452;

        public const int CONTROLLER_TYPE_5UT = 0x0055;
        public const int CONTROLLER_TYPE_5U0 = 0x0152;
        public const int CONTROLLER_TYPE_5U1 = 0x0255;
        public const int CONTROLLER_TYPE_5U2 = 0x0355;
        public const int CONTROLLER_TYPE_5U3 = 0x0455;
        public const int CONTROLLER_TYPE_5U4 = 0x0555;

        public const int CONTROLLER_TYPE_5E2 = 0x0254;
        public const int CONTROLLER_TYPE_5E3 = 0x0354;


        //------------------------------------------------------------------------------

        #endregion

        #region SEND_CMD

        //==============================================================================
        // Controller communication mode
        public const int SEND_MODE_COMM = 0;
        public const int SEND_MODE_NET = 2;
        //==============================================================================

        //==============================================================================
        // User send a message command table
        public const int SEND_CMD_PARAMETER = 0xA1FF; // load screen parameters.
        public const int SEND_CMD_SCREENSCAN = 0xA1FE; // set the scanning mode.
        public const int SEND_CMD_SENDALLPROGRAM = 0xA1F0; // send all program information.
        public const int SEND_CMD_POWERON = 0xA2FF; // Force power
        public const int SEND_CMD_POWEROFF = 0xA2FE; // forced shutdown
        public const int SEND_CMD_TIMERPOWERONOFF = 0xA2FD; // timer switch
        public const int SEND_CMD_CANCEL_TIMERPOWERONOFF = 0xA2FC; // cancel the timer switch
        public const int SEND_CMD_RESIVETIME = 0xA2FB; // correction time.
        public const int SEND_CMD_ADJUSTLIGHT = 0xA2FA; // brightness adjustment.
        //==============================================================================

        #endregion

        #region ORTHER PARAMETERS

        //==============================================================================
        // SCREEN TYPE COLOR
        public const int SCREEN_TYPE_SINGLECOLOR = 1;
        public const int SCREEN_TYPE_DUALCOLOR = 2;
        public const int SCREEN_TYPE_RGBCOLOR = 3;      // Only BX-4MC Support
       
        // PIXEL MODE support dual color.
        public const int PIXCEL_MODE_RG = 1;
        public const int PIXCEL_MODE_GR = 2;

        // Data DA OE
        public const int DATA_DA_NEGATIVE = 0x00;
        public const int DATA_DA_POSITYPE = 0x01;
        public const int DATA_OE_LOW = 0x00;
        public const int DATA_OE_HIGHT = 0x01;

        // Font Color
        public const int FONT_COLOR_BLACK = 0;
        public const int FONT_COLOR_RED = 1;
        public const int FONT_COLOR_GREEN = 2;
        public const int FONT_COLOR_YELLOW = 3;
        public const int FONT_COLOR_BLUE = 4;
        public const int FONT_COLOR_MAUVE = 5;
        public const int FONT_COLOR_LIGHTGREEN = 6;
        public const int FONT_COLOR_WHITE = 7;

        // Row Mode
        public const int ROW_ORDER_NORMAL = 0;  // Default
        public const int ROW_ORDER_ADDONELINE = 1;
        public const int ROW_ORDER_DELETEONELINE = 2;

        // Scan frequency default
        public const int SCAN_FREQUENCY_DEFAULT = 0;
        // Default Com
        public const string SERIAL_PORT_NAME_1 = "Com1";
        public const int SERIAL_PORT_BAUD_RATE_57600 = 57600;
        public const int SERIAL_PORT_BAUD_RATE_9600 = 9600;

        // Default Screen Port
        public const int SCREEN_PORT = 5005;
        //==============================================================================

        #endregion

        #region DISPLAY_MODE

        //==============================================================================
        // Display Mode
        public const int DISPLAY_MODE_RANDOM = 0x00;
        public const int DISPLAY_MODE_STATIC = 0x01;
        public const int DISPLAY_MODE_DIRECTSHOW = 0x02;

        public const int DISPLAY_MODE_MOVELEFT = 0x03;
        public const int DISPLAY_MODE_CONTINUEMOVELEFT = 0x04;
        public const int DISPLAY_MODE_MOVEUP = 0x05;    // Bx-3T do not support
        public const int DISPLAY_MODE_CONTINUEMOVEUP = 0x06;    // BX-3T do not support

        public const int DISPLAY_MODE_FLICKER = 0x07;           // BX-3T do not support
        public const int DISPLAY_MODE_SNOWING = 0x08;
        public const int DISPLAY_MODE_BUBBLING = 0x09;
        public const int DISPLAY_MODE_MIDLEOUT = 0x0A;
        public const int DISPLAY_MODE_MOVEAROUND = 0x0B;

        public const int DISPLAY_MODE_HORIZONTALCROSSMOVE = 0x0C;
        public const int DISPLAY_MODE_VERTICALCROSSMOVE = 0x0D;

        public const int DISPLAY_MODE_SCROLLCLOSED = 0x0E;
        public const int DISPLAY_MODE_SCROOLOPENED = 0x0F;

        public const int DISPLAY_MODE_LEFTSTRETCH = 0x10;
        public const int DISPLAY_MODE_RIGHTSTRETCH = 0x11;
        public const int DISPLAY_MODE_UPSTRETCH = 0x12;
        public const int DISPLAY_MODE_DOWNSTRETCH = 0x13;   // BX-3T do not support

        public const int DISPLAY_MODE_LEFTLASER = 0x14;
        public const int DISPLAY_MODE_RIGHTLASER = 0x15;
        public const int DISPLAY_MODE_UPLASER = 0x16;
        public const int DISPLAY_MODE_DOWNLASER = 0x17;

        public const int DISPLAY_MODE_CROSSCURTAINLEFTRIGHT = 0x18;
        public const int DISPLAY_MODE_CROSSCURTAINUPDOWN = 0x19; // 3T 、 3A 、 4A 、 3A1 、 3A2 、 4A1 、 4A2 、 4A3 、 4AQ do not suport
        public const int DISPLAY_MODE_CURTAINSCATTEREDtotheTHELEFT = 0x1A; // 3T 、 3A 、 4A 、 3A1 、 3A2 、 4A1 、 4A2 、 4A3 、 4AQ do not suport
        
        public const int DISPLAY_MODE_HORIZONTALBLINDS = 0x1B;
        public const int DISPLAY_MODE_VERTICALBLINDS = 0x1C; // 3M, 4M, 4M1, 4MC do not support.
        
        public const int DISPLAY_MODE_CURTAINLEFT = 0x1D;   // 3T, 3A, 4A do not support
        public const int DISPLAY_MODE_CURTAINRIGHT = 0x1E;   // 3T, 3A, 4A do not support
        public const int DISPLAY_MODE_CURTAINUP = 0x1F;   // 3T, 3A, 4A do not support
        public const int DISPLAY_MODE_CURTAINDOWN = 0x20;   // 3T, 3A, 4A do not support
        
        public const int DISPLAY_MODE_MOVEtoCENTERfromLEFTandRIGHT = 0x21; // 3T do not support
        public const int DISPLAY_MODE_SPLITtoLEFTandRIGHT = 0x22; // 3T do not support
        
        public const int DISPLAY_MODE_MOVEtoCENTERfromUPandDOWN = 0x23; // 3T do not support
        public const int DISPLAY_MODE_SPLITtoUPandDOWN = 0x24; // 3T do not support
        
        public const int DISPLAY_MODE_MOVERIGHT = 0x25;
        public const int DISPLAY_MODE_CONTINUEMOVERIGHT = 0x26;
        public const int DISPLAY_MODE_MOVEDOWN = 0x27;  // 3T Do not support
        public const int DISPLAY_MODE_CONTINUEMOVEDOWN = 0x28; // 3T do not support
        //==============================================================================

        #endregion
    
    }

}